package com.ocom.webapi.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.request.web.*;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.*;
import com.ocom.common.vo.recode.AccessRecordVo;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.MjRecodeClientService;
import com.ocom.webapi.service.AccessService;
import com.ocom.webapi.vo.access.AccessDeviceResetVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class AccessController {

    @Autowired
    private AccessService accessService;

    @Autowired
    private MjRecodeClientService mjRecodeClientService;
    /**
     * 区域管理列表（AreaPage/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_AreaPageOnShow)
    public Result AreaPageOnShow(@Validated @RequestBody AreaVo areaVo){
        return accessService.AreaPageOnShow(areaVo);
    }


    /**
     * 区域新增
     * */
    @PostMapping(AccessServiceUrl.URL_AreaPageAdd)
    public Result AreaPageAdd(@Validated @RequestBody AddAreaVo addAreaVo){
        return accessService.AreaPageAdd(addAreaVo);
    }


    /**
     * 区域修改
     * */
    @PostMapping(AccessServiceUrl.URL_AreaPageModify)
    public Result AreaPageModify(@Validated @RequestBody ModifyAreaVo modifyAreaVo){
        return accessService.AreaPageModify(modifyAreaVo);
    }


    /**
     * 区域删除
     * */
    @PostMapping(AccessServiceUrl.URL_AreaPageDelete)
    public Result AreaPageDelete(@Validated @RequestBody DelAreaVo delAreaVo){
        return accessService.AreaPageDelete(delAreaVo);
    }


    /**
     * 门禁设备列表（/AccessDevice/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceOnShow)
    public Result AccessDeviceOnShow(@Validated @RequestBody DeviceOnShowVo deviceOnShowVo){
        log.error("门禁设备列表（/AccessDevice/onShow 【{}】",deviceOnShowVo);
        return accessService.AccessDeviceOnShow(deviceOnShowVo);
    }


    /**
     * 门禁设备新增（/AccessDevice/add）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceAdd)
    public Result AccessDeviceAdd(@Validated @RequestBody AccessDeviceVo accessDeviceVo){
        return accessService.AccessDeviceAdd(accessDeviceVo);
    }


    /**
     * 门禁设备新增/修改（AccessDevice/modify）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceModify)
    public Result AccessDeviceModify(@Validated @RequestBody AccessDeviceVo accessDeviceVo){

        return accessService.AccessDeviceModify(accessDeviceVo);
    }


    /**
     * 门禁设备区域设置（AccessDevice/setArea）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceSetArea)
    public Result AccessDeviceSetArea(@Validated @RequestBody SetAreaVo setAreaVo){
        return accessService.AccessDeviceSetArea(setAreaVo);
    }

    /**
     * 门禁设备删除（AccessDevice/delete）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceDelete)
    public Result AccessDeviceDelete(@Validated @RequestBody AccessDeviceVo accessDeviceVo){

        return accessService.AccessDeviceDelete(accessDeviceVo);
    }

    /**
     * 门禁设备列表（AccessDevice/allOnShow）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceAllOnShow)
    public Result AccessDeviceAllOnShow(@Validated @RequestBody AllOnShowVo allOnShowVo){
        return accessService.AccessDeviceAllOnShow(allOnShowVo);
    }


    /*
     * @Description:人员门禁权限列表
     * @Author: lu 
     * @Date: 2022/1/12 10:55
     * @param allOnShowVo:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_webGetAccessByUser)
    public Result getAccessByUser(@Valid @RequestBody GetAccessByUserRequest getAccessByUserRequest){
        return accessService.getAccessByUser(getAccessByUserRequest);
    }

    /*
     * @Description:人员门禁权限新增
     * @Author: lu 
     * @Date: 2022/1/13 8:53
     * @param getAccessRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_webAccessControlPersonAdd)
    public Result setAccessAdd(@Valid @RequestBody GetAccessRequest getAccessRequest){
        return accessService.setAccessAdd(getAccessRequest);
    }

    /*
     * @Description:人员门禁权限修改
     * @Author: lu 
     * @Date: 2022/1/13 8:53
     * @param getAccessRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_webAccessControlPersonModify)
    public Result setAccessModify(@Valid @RequestBody GetAccessRequest getAccessRequest){
        return accessService.setAccessAdd(getAccessRequest);
    }

    /*
     * @Description:时区管理列表
     * @Author: lu 
     * @Date: 2022/1/13 17:14
     * @param getZoneDateListRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_getZoneListOnShow)
    public Result getZoneDateList(@RequestBody GetZoneDateListRequest getZoneDateListRequest){
        Result r=accessService.getZoneDateList(getZoneDateListRequest);
        Map<String,Object> map=new HashMap<>();
        if (r.getCode()==200){
            JSONArray jsonArray= JSON.parseArray(JSON.toJSONString( r.getData()));
            List<Object> zoneDateList0=new ArrayList<>();
            List<Object> zoneDateList1=new ArrayList<>();
            for (Object o : jsonArray) {
                Map<String,Object> zoneMap=JSON.parseObject(JSON.toJSONString(o),Map.class);
                Integer dayDate= (Integer) zoneMap.get("dayDate");
                if (dayDate==1){
                    zoneDateList1.add(o);
                }else {
                    zoneDateList0.add(o);
                }
            }
            map.put("dayList",zoneDateList0);
            map.put("dateList",zoneDateList1);
        }
        Result zoneR= accessService.getZoneList(getZoneDateListRequest.getComId());
        if (zoneR.getCode()==200){
            map.put("zoneList",zoneR.getData());
        }
        return ResultUtil.success(map);
    }

    /**
     * 导入机器（AccessDevice/importDevice）
     * */
    @SneakyThrows
    @PostMapping(AccessServiceUrl.URL_AccessDeviceImportDevice)
    public Result AccessDeviceImportDevice( Long comId, Long deviceType,@RequestPart("file") MultipartFile file) {
        //判断是否为excel类型文件

        return accessService.AccessDeviceImportDevice(comId,deviceType,file);

    }

    /*
     * @Description:设置日期时区新增
     * @Author: lu 
     * @Date: 2022/1/14 14:15
     * @param getZoneDateListRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_setDateZoneAdd)
    public Result setDateZoneAdd(@RequestBody SetDateZoneRequest setDateZoneRequest){
        return accessService.setDateZoneAdd(setDateZoneRequest);
    }

    /*
     * @Description:设置日期时区修改
     * @Author: lu
     * @Date: 2022/1/14 14:34
     * @param setDateZoneRequest:
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_setDateZoneModify)
    public Result setDateZoneModify(@RequestBody SetDateZoneRequest setDateZoneRequest){
        return accessService.setDateZoneAdd(setDateZoneRequest);
    }

    /*
     * @Description:设置日期时区修改
     * @Author: lu
     * @Date: 2022/1/14 14:34
     * @param setDateZoneRequest:
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_removeDateZoneDelete)
    public Result removeDateZone(@RequestBody RemoveDateZoneRequest removeDateZoneRequest){
        return accessService.removeDateZone(removeDateZoneRequest);
    }






    /*
     * @Description:2.5.3.4.区域权限组列表（AccessControlPerson/groupList）
     * @Author: lu
     * @Date: 2022/1/18 15:18
     * @param map:
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_AccessControlPersonGroupList)
    public Result getGroupListByArea(@RequestBody Map<String,String> map){
        return accessService.getGroupListByArea(map);
    }

    /**获取区域信息*/
    @GetMapping(AccessServiceUrl.URL_AccessGetAreaInfo)
    public Result AccessGetAreaInfo(Long comId,Integer regionId){
        return accessService.AccessGetAreaInfo(comId,regionId);
    }

    /**
     *  门禁记录（AccessRecord/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessRecordOnShow)
    public Result AccessRecordOnShow(@RequestBody @Validated AccessRecordVo accessRecordVo){
        return mjRecodeClientService.GetAccessRecord(accessRecordVo);
       // return accessService.AccessRecordOnShow(onShowVo);
    }

    /**
     * 区域权限列表 （AccessControlPerson/getAreaGroupList）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessControlPersonGetAreaGroupList)
    public Result AccessControlPersonGetAreaGroupList(){
        Long companyId = SecurityUtils.getYoCiUser().getComId();
        return accessService.AccessControlPersonGetAreaGroupList(companyId);
    }
    /**
     * 重置设备白名单（AccessDevice/reset）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessDeviceReset)
    public Result AccessDeviceReset(@Validated @RequestBody AccessDeviceResetVo accessDeviceResetVo){
        log.info("重置设备白名单请求参数:{{}}",accessDeviceResetVo);

        return accessService.AccessDeviceReset(accessDeviceResetVo);
    }


    //importAreaAccess
    @PostMapping(WebApiServiceUrl.URL_importAreaAccess)
    public Object importAreaAccess(Long comId, Long areaNo,Integer type,@RequestPart("file") MultipartFile file) {
        return accessService.importAreaAccess(comId,areaNo,type,file) ;
    }

}
